﻿
Partial Class pages_products_ProductVimeo : Inherits CPage

#Region "Querystring"
    Public ReadOnly Property ProductId() As Integer
        Get
            Return CWeb.RequestInt("productId")
        End Get
    End Property
#End Region

#Region "Data"
    Public ReadOnly Property [Product]() As CProduct
        Get
            Product = CProduct.Cache.GetById(ProductId)
            If IsNothing(Product) Then CSitemap.RecordNotFound("Product", ProductId)
        End Get
    End Property
#End Region

#Region "Navigation"
    Private Sub Refresh()
        Response.Redirect(CSitemap.ProductVimeo(Me.Product.ProductId))
    End Sub
    Private Sub ReturnToList()
        Response.Redirect(CSitemap.Products(Nothing, Product.ProductTypeId, Integer.MinValue, Integer.MinValue, Integer.MinValue, Integer.MinValue, Integer.MinValue, Nothing))
    End Sub
#End Region

#Region "Event Handlers - Page"
    Protected Overrides Sub PageInit()
        With Product
            MenuSelected = "Products"
            UnbindSideMenu()
            AddMenuSide(.ProductType.Plural & "...", CSitemap.Products(Nothing, Product.ProductTypeId, Integer.MinValue, Integer.MinValue, Integer.MinValue, Integer.MinValue, Integer.MinValue, Nothing))
            AddMenuSide(Product.TypeName & " Details", CSitemap.ProductEdit(ProductId))
            AddMenuSide("Image" & IIf(.HasImage, "*", String.Empty), CSitemap.ProductImage(ProductId))
            If .IsSet Then AddMenuSide(CUtilities.NameAndCount("Contents", .Children), CSitemap.ProductContents(ProductId))
            If .CanHaveTranscript Then
                AddMenuSide("Transcription" & IIf(.HasTranscript, "*", String.Empty), CSitemap.ProductTranscription(ProductId))
            End If
            AddMenuSide("Vimeo Link" & IIf(.HasVideosOnVimeo, "*", String.Empty) & IIf(.HasVideosOnYouTube, "*", String.Empty), CSitemap.ProductVimeo(ProductId), True)
            If .CanHaveFiles Then
                AddMenuSide("Dropbox Files", CSitemap.ProductFiles(ProductId))
            Else
                AddMenuSide(CUtilities.NameAndCount("Epub/Mobi eBooks", CFile.Cache.GetByProductId(Me.ProductId)), CSitemap.ProductFiles(ProductId))
            End If
            If Not .IsFree OrElse .PaymentsCount > 0 OrElse .OrdersCountCached > 0 Then
                AddMenuSide(CUtilities.NameAndCount("Orders", .OrdersCountCached), CSitemap.OrdersForProduct(ProductId))
                AddMenuSide(CUtilities.NameAndCount("Payments", .PaymentsCount), CSitemap.PaymentsForProduct(ProductId))
            End If
            If .HasParent Then AddMenuSide("Back to Set...", CSitemap.ProductContents(.ProductBelongsToProductId))

            litPlaylist.Visible = .IsSet
            litChannel.Visible = .IsSet

            If .IsSet Then
                txtVimeo.Value = .ProductVimeoChannelId
                txtYouTube.Value = .ProductYouTubePlaylistId
            End If

            If .ProductVimeoId > 0 Then
                txtVimeo.Value = .ProductVimeoId
                iframeVimeo.Visible = True
                iframeVimeo.Attributes("src") &= .ProductVimeoId
            End If
            If Not String.IsNullOrEmpty(.ProductYouTubeId) Then
                txtYouTube.Value = .ProductYouTubeId
                iframeYouTube.Visible = True
                iframeYouTube.Attributes("src") &= .ProductYouTubeId
            End If
        End With
    End Sub
#End Region


    Protected Sub btnSave_Click(sender As Object, e As System.EventArgs) Handles btnSave.Click
        With Product
            If .IsSet Then
                .ProductVimeoChannelId = txtVimeo.Value
                .ProductYouTubePlaylistId = txtYouTube.Value

                If txtVimeo.Text.ToLower.Contains("channels/") Then
                    .ProductVimeoChannelId = txtVimeo.Text.Substring(txtVimeo.Text.IndexOf("channels/") + 9)
                ElseIf txtYouTube.Value.ToLower.Contains("list=") Then
                    .ProductYouTubePlaylistId = txtYouTube.Text.Substring(txtYouTube.Text.IndexOf("list=") + 5)
                End If
            Else
                .ProductVimeoId = txtVimeo.ValueInt
                .ProductYouTubeId = txtYouTube.Value
                If txtVimeo.ValueInt < 0 AndAlso txtVimeo.Text.ToLower.Contains(".com/") Then
                    Integer.TryParse(txtVimeo.Text.Substring(txtVimeo.Text.IndexOf(".com/") + 5), .ProductVimeoId)
                ElseIf txtYouTube.Value.ToLower.Contains("youtu.be") Then
                    .ProductYouTubeId = txtYouTube.Text.Substring(txtYouTube.Text.LastIndexOf("/") + 1)
                ElseIf txtYouTube.Value.Contains(".com") Then
                    Dim startAt As Integer = txtYouTube.Value.IndexOf("v=") + 2
                    Dim stopAt As Integer = txtYouTube.Value.IndexOf("&")
                    If stopAt > 0 Then
                        .ProductYouTubeId = txtYouTube.Value.Substring(startAt, stopAt - startAt)
                    ElseIf startAt > 0 Then
                        .ProductYouTubeId = txtYouTube.Value.Substring(startAt)
                    End If
                End If
            End If

            .Save()
        End With
        Refresh()
    End Sub

End Class
